package com.linjiu.xpermissionclent;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

/**
 * 项目名称：com.ebeitech.mPaaSDemo.launcher.util.permission.client
 * 创建人:LuoFG
 * 创建时间:2022/12/14  15:19
 * 描述:
 * 作用:
 */
public class PermissionPopWindow {
    private TextView tvTitle,tvContent;
    private LinearLayout lineAll;
    private Context mContext;
    private ViewGroup decorView;
    /** 权限申请说明 Popup */
    private PopupWindow mPermissionPopup;



    public PermissionPopWindow(Context mContext, ViewGroup decorView) {
        this.mContext = mContext;
        this.decorView=decorView;
        initView();
    }
    

    private void initView() {
        if (mPermissionPopup == null) {
            View contentView = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_permiss_tips, decorView, false);
            mPermissionPopup = new PopupWindow(mContext);
            mPermissionPopup.setContentView(contentView);
            mPermissionPopup.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
            mPermissionPopup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
            mPermissionPopup.setAnimationStyle(R.style.DialogAnimationFade);
            mPermissionPopup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            mPermissionPopup.setTouchable(true);
            mPermissionPopup.setOutsideTouchable(true);
        }

        tvTitle= (TextView) mPermissionPopup.getContentView().findViewById(R.id.tv_title);
        tvContent= (TextView) mPermissionPopup.getContentView().findViewById(R.id.tv_content);
        lineAll= (LinearLayout) mPermissionPopup.getContentView().findViewById(R.id.lineAll);
        lineAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }



    public PermissionPopWindow setTitle(String title){
        tvTitle.setText(title);
        return this;
    }

    public PermissionPopWindow setContent(String title){
        tvContent.setText(title);
        return this;
    }


    /**
     * 对话框显示
     */
    public PermissionPopWindow show() {
        try {
            if (mContext == null) {
                return this;
            }

            if (mPermissionPopup == null || mPermissionPopup.isShowing()) {
                return this;
            }
            // 注意：这里的 PopupWindow 只是示例，没有监听 Activity onDestroy 来处理 PopupWindow 生命周期
            mPermissionPopup.showAtLocation(decorView, Gravity.TOP, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    public boolean isShow(){
        if(mPermissionPopup==null){
            return false;
        }
        return mPermissionPopup.isShowing();
    }


    /**
     * 对话框关闭
     */
    public PermissionPopWindow dismiss() {
        try {
            if (mContext == null) {
                return this;
            }
            if (!mPermissionPopup.isShowing()) {
                return this;
            }

            mPermissionPopup.dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }




}
